package entity

import (
	"bytes"
	"strings"
	"time"

	"shqsoft.cn/pasnj/base"
)

//TIndiClie 用户
type TUserDiviChange struct {
	ID           int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	EMID         int        `gorm:"column:EMID"`
	User         User       `gorm:"ForeignKey:EMID"`
	EMID1        int        `gorm:"column:EMID1"`
	DSVL1        float64    `gorm:"column:DSVL1"`
	User1        User       `gorm:"ForeignKey:EMID1"`
	EMID2        *int       `gorm:"column:EMID2"`
	DSVL2        *float64   `gorm:"column:DSVL2"`
	User2        User       `gorm:"ForeignKey:EMID2"`
	EMID3        *int       `gorm:"column:EMID3"`
	DSVL3        *float64   `gorm:"column:DSVL3"`
	User3        User       `gorm:"ForeignKey:EMID3"`
	EMID4        *int       `gorm:"column:EMID4"`
	DSVL4        *float64   `gorm:"column:DSVL4"`
	User4        User       `gorm:"ForeignKey:EMID4"`
	EMID5        *int       `gorm:"column:EMID5"`
	DSVL5        *float64   `gorm:"column:DSVL5"`
	User5        User       `gorm:"ForeignKey:EMID5"`
	ZCDT         *time.Time `gorm:"column:ZCDT"`
	ZCYJ         string     `gorm:"column:ZCYJ;size:50"`
	ZCYJCaptions string     `gorm:"-"`
}

//TableName User用户表名
func (TUserDiviChange) TableName() string {
	return "T_USER_DIVI_CHANGE"
}

func (m *TUserDiviChange) AfterFind() (err error) {
	if m.ZCYJ != "" {
		ids := strings.Split(m.ZCYJ, ",")
		sql := "SELECT NAME FROM (select 1 ID,'对公帐户业绩' NAME UNION ALL SELECT 2 ,'对私帐户业绩') A WHERE ID IN (?)"
		rows, err := base.DB.Raw(sql, ids).Rows()
		if err != nil {
			return err
		}
		var buff bytes.Buffer
		defer rows.Close()
		for rows.Next() {
			var caption string
			rows.Scan(&caption)
			buff.WriteString(caption)
			buff.WriteString(",")
		}
		if l := buff.Len(); l > 0 {
			buff.Truncate(l - 1)
		}
		m.ZCYJCaptions = buff.String()
	}
	return
}

func TUserDiviChangeEF() base.EntityFunc {
	return func() interface{} {
		entity := TUserDiviChange{}
		return &entity
	}
}
func TUserDiviChangeESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []TUserDiviChange
		return &entities
	}
}

func init() {
	relationship := []string{"User", "User1", "User2", "User3", "User4", "User5"}
	base.DaoPoolObj.Put("TUserDiviChange", base.NewDefaultDao(TUserDiviChangeEF(), TUserDiviChangeESF(), relationship))
}
